package com.smart.carey.util;

import com.smart.carey.model.ControllerModel;
import com.smart.carey.model.HttpServiceDOC;
import com.smart.carey.model.ParameterModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Method;
import java.util.*;

public class ClassUtil {

    private static Logger logger = LoggerFactory.getLogger(BeanUtils.class);

    public static void main(String[] args) {
        ClassUtil classUtil = new ClassUtil();
        try {
            List<ControllerModel> modelList = classUtil.getControllerInfo("com.smart.bspoj.web");
            System.out.println(modelList);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public List<ControllerModel> getControllerInfo(String scan) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        logger.info("服务名称加载中..");
        String path = this.getClass().getResource("/").getPath();

        String scanPath = scan.replaceAll("\\.", "/");
        logger.info("最终读取路径：{}", path + scanPath);

        File file = new File(path + scanPath);
        List<String> clazzList = Arrays.asList(file.list());
        List<ControllerModel> controllerModels = new ArrayList<>();
        for (String clazz : clazzList) {
            clazz = "." + clazz.replaceAll("\\.class", "");

            Object o = Class.forName(scan + clazz).newInstance();
//            if (o.getClass().isAnnotationPresent(HttpServiceDOC.class)) {
            List<Method> methodList = Arrays.asList(o.getClass().getDeclaredMethods());
            for (Method method : methodList) {
                if (method.isAnnotationPresent(HttpServiceDOC.class)) {
                    HttpServiceDOC httpServiceDOC = method.getDeclaredAnnotation(HttpServiceDOC.class);
                    String parameters = httpServiceDOC.parameter();
                    String description = httpServiceDOC.description();
                    String example = httpServiceDOC.example();
                    String url = httpServiceDOC.url();
                    int sort = httpServiceDOC.sort();

                    ControllerModel controllerModel = new ControllerModel();
                    List<String> list = new ArrayList<>(
                            Arrays.asList(parameters.split("@")));
                    List<ParameterModel> parameterModels = new ArrayList<>();
                    //字段描述信息
                    try {
                        for (String line : list) {
                            line = line.trim();
                            line = line.replaceAll("\\s*", "");
                            if (!"".equals(line)) {
                                String[] attributes = line.split("\\|");
                                ParameterModel parameterModel = new ParameterModel();
                                parameterModel.setType(attributes[0]);
                                parameterModel.setName(attributes[1]);
                                parameterModel.setParameterDesc(attributes[2]);
                                parameterModel.setMust(attributes[3]);
                                parameterModels.add(parameterModel);
                            }
                        }
                    } catch (ArrayIndexOutOfBoundsException e) {
                        logger.error("类:" + o + "方法：" + method.getName());
                        e.printStackTrace();
//                        System.exit(0);
                    }

                    controllerModel.setParameter(parameterModels);
                    controllerModel.setExample(example);
                    controllerModel.setDescription(description);
                    controllerModel.setUrl(url);
                    controllerModel.setSort(sort);
                    controllerModels.add(controllerModel);

                }
            }

        }
        controllerModels.sort(Comparator.comparing(ControllerModel::getSort));
        return controllerModels;
    }

}
